home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_190 / nethack / twee.zoo / pager.c < prev    next >
C/C++ Source or Header  |  1988-07-24  |  11KB  |  502 lines

  1. /*    SCCS Id: @(#)pager.c    2.3     87/12/12
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  
  4. /* This file contains the command routine dowhatis() and a pager. */
  5. /* Also readmail() and doshell(), and generally the things that
  6.    contact the outside world. */
  7.  
  8. #include    <stdio.h>
  9. #include    <signal.h>
  10. #include     "hack.h"
  11.  
  12. #ifdef AMIGA        /* same as in amigaTermcap.c */
  13. # define xputc(c)   WindowPutchar(c)
  14. # define xputs(s)   WindowFPuts(s)
  15. #endif
  16.  
  17. extern int CO, LI;    /* usually COLNO and ROWNO+2 */
  18. extern char *CD;
  19. extern char quitchars[];
  20. extern char *getenv(), *getlogin();
  21. extern xchar curx;
  22. int done1();
  23.  
  24. dowhatis()
  25. {
  26.     FILE *fp;
  27.     char bufr[BUFSZ+6];
  28.     register char *buf = &bufr[6], *ep, q;
  29.     extern char readchar();
  30.  
  31.     if(!(fp = fopen(DATAFILE, "r")))
  32.         pline("Cannot open data file!");
  33.     else {
  34. #ifndef GRAPHICS
  35.         pline("Specify what? ");
  36.         q = readchar();
  37. #else
  38.         extern getpos();
  39.         coord    cc;
  40.         unsigned char r;    /* OIS, for chars >= 128 */
  41.         /* q need not be unsigned, it is same type as buf. */
  42.  
  43.         pline ("Specify unknown object by cursor ? [ynq] ");
  44.         while(!index("yYnNqQ", (q = readchar())) &&
  45.                           !index(quitchars, q))     bell();
  46.  
  47.         if (q == 'n' || q == 'N') {
  48.             pline("Specify what? ");
  49.             r = readchar();
  50.         } else if (index(quitchars, q))
  51.             r = q;
  52.         else {
  53.             pline("Please move the cursor to the unknown object.");
  54.             getpos(&cc, TRUE, "the unknown object");
  55.             r = levl[cc.x][cc.y].scrsym;
  56.         }
  57.  
  58.         if (r == showsyms.stone) q = defsyms.stone;
  59.         else if (r == showsyms.vwall) q = defsyms.vwall;
  60.         else if (r == showsyms.hwall) q = defsyms.hwall;
  61.         else if (r == showsyms.tlcorn) q = defsyms.tlcorn;
  62.         else if (r == showsyms.trcorn) q = defsyms.trcorn;
  63.         else if (r == showsyms.blcorn) q = defsyms.blcorn;
  64.         else if (r == showsyms.brcorn) q = defsyms.brcorn;
  65.         else if (r == showsyms.door) q = defsyms.door;
  66.         else if (r == showsyms.room) q = defsyms.room;
  67.         else if (r == showsyms.corr) q = defsyms.corr;
  68.         else if (r == showsyms.upstair) q = defsyms.upstair;
  69.         else if (r == showsyms.dnstair) q = defsyms.dnstair;
  70.         else if (r == showsyms.trap) q = defsyms.trap;
  71. #ifdef FOUNTAINS
  72.         else if (r == showsyms.pool) q = defsyms.pool;
  73.         else if (r == showsyms.fountain) q = defsyms.fountain;
  74. #endif
  75. #ifdef NEWCLASS
  76.         else if (r == showsyms.throne) q = defsyms.throne;
  77. #endif
  78. #ifdef SPIDERS
  79.         else if (r == showsyms.web) q = defsyms.web;
  80. #endif
  81. #ifdef SINKS
  82.         else if (r == showsyms.sink) q = defsyms.sink;
  83. #endif
  84.         else
  85.             q = r;
  86. #endif /* GRAPHICS */
  87. #ifdef DGKMOD
  88.         if (index(quitchars, q)) {
  89.             (void) fclose(fp); /* sweet@scubed */
  90.             return(0);
  91.         }
  92. #endif
  93. #ifdef KJSMODS
  94.         if(q == '%') {
  95.             pline("%%       a piece of food");
  96.             (void) fclose(fp);
  97.             return(0);
  98.         }
  99. #endif
  100.         if(q != '\t')
  101.         while(fgets(buf,BUFSZ,fp))
  102.             if(*buf == q) {
  103.             ep = index(buf, '\n');
  104.             if(ep) *ep = 0;
  105.             /* else: bad data file */
  106.             /* Expand tab 'by hand' */
  107.             if(buf[1] == '\t'){
  108.                 buf = bufr;
  109. #ifdef GRAPHICS
  110.                 buf[0] = r;
  111. #else
  112.                 buf[0] = q;
  113. #endif
  114.                 (void) strncpy(buf+1, "       ", 7);
  115.             }
  116.             pline(buf);
  117.             if(ep[-1] == ';') {
  118.                 pline("More info? ");
  119.                 if(readchar() == 'y') {
  120.                     page_more(fp,1); /* does fclose() */
  121.                     return(0);
  122.                 }
  123.             }
  124.             (void) fclose(fp);      /* kopper@psuvax1 */
  125.             return(0);
  126.             }
  127.         pline("I've never heard of such things.");
  128.         (void) fclose(fp);
  129.     }
  130.     return(0);
  131. }
  132.  
  133. /* make the paging of a file interruptible */
  134. static int got_intrup;
  135.  
  136. intruph(){
  137.     got_intrup++;
  138. }
  139.  
  140. /* simple pager, also used from dohelp() */
  141. page_more(fp,strip)
  142. FILE *fp;
  143. int strip;    /* nr of chars to be stripped from each line (0 or 1) */
  144. {
  145.     register char *bufr, *ep;
  146. #ifdef DGK
  147.     /* There seems to be a bug in ANSI.SYS    The first tab character
  148.      * after a clear screen sequence is not expanded correctly.  Thus
  149.      * expand the tabs by hand -dgk
  150.      */
  151.     int tabstop = 8, spaces;
  152.     char buf[BUFSIZ], *bufp, *bufrp;
  153.  
  154.     set_pager(0);
  155.     bufr = (char *) alloc((unsigned) CO);
  156.     while (fgets(buf, BUFSIZ, fp) && (!strip || *buf == '\t')){
  157.         bufp = buf;
  158.         bufrp = bufr;
  159.         while (*bufp && *bufp != '\n') {
  160.             if (*bufp == '\t') {
  161.                 spaces = tabstop - (bufrp - bufr) % tabstop;
  162.                 while (spaces--)
  163.                     *bufrp++ = ' ';
  164.                 bufp++;
  165.             } else
  166.                 *bufrp++ = *bufp++;
  167.         }
  168.         *bufrp = '\0';
  169. #else
  170.     int (*prevsig)() = signal(SIGINT, intruph);
  171.  
  172.     set_pager(0);
  173.     bufr = (char *) alloc((unsigned) CO);
  174.     bufr[CO-1] = 0;
  175.     while(fgets(bufr,CO-1,fp) && (!strip || *bufr == '\t')){
  176.         ep = index(bufr, '\n');
  177.         if(ep)
  178.             *ep = 0;
  179. #endif /* DGK /**/
  180.         if(page_line(bufr+strip)) {
  181.             set_pager(2);
  182.             goto ret;
  183.         }
  184.     }
  185.     set_pager(1);
  186. ret:
  187.     free(bufr);
  188.     (void) fclose(fp);
  189. #ifndef DGK
  190.     (void) signal(SIGINT, prevsig);
  191.     got_intrup = 0;
  192. #endif
  193. }
  194.  
  195. static boolean whole_screen = TRUE;
  196. #define PAGMIN    12    /* minimum # of lines for page below level map */
  197.  
  198. set_whole_screen() {    /* called in termcap as soon as LI is known */
  199.     whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
  200. }
  201.  
  202. #ifdef NEWS
  203. readnews() {
  204.     register int ret;
  205.  
  206.     whole_screen = TRUE;    /* force a docrt(), our first */
  207.     ret = page_file(NEWS, TRUE);
  208.     set_whole_screen();
  209.     return(ret);            /* report whether we did docrt() */
  210. }
  211. #endif
  212.  
  213. set_pager(mode)
  214. register int mode;    /* 0: open  1: wait+close  2: close */
  215. {
  216.     static boolean so;
  217.     if(mode == 0) {
  218.         if(!whole_screen) {
  219.             /* clear topline */
  220.             clrlin();
  221.             /* use part of screen below level map */
  222.             curs(1, ROWNO+4);
  223.         } else {
  224.             cls();
  225.         }
  226.         so = flags.standout;
  227.         flags.standout = 1;
  228.     } else {
  229.         if(mode == 1) {
  230.             curs(1, LI);
  231.             more();
  232.         }
  233.         flags.standout = so;
  234.         if(whole_screen)
  235.             docrt();
  236.         else {
  237.             curs(1, ROWNO+4);
  238.             cl_eos();
  239.         }
  240.     }
  241. }
  242.  
  243. page_line(s)            /* returns 1 if we should quit */
  244. register char *s;
  245. {
  246.     extern char morc;
  247.  
  248.     if(cury == LI-1) {
  249.         if(!*s)
  250.             return(0);      /* suppress blank lines at top */
  251.         putchar('\n');
  252.         cury++;
  253.         cmore("q\033");
  254.         if(morc) {
  255.             morc = 0;
  256.             return(1);
  257.         }
  258.         if(whole_screen)
  259.             cls();
  260.         else {
  261.             curs(1, ROWNO+4);
  262.             cl_eos();
  263.         }
  264.     }
  265. #ifdef TERMINFO
  266.     xputs(s); xputc('\n');
  267. #else
  268.     puts(s);
  269. #endif
  270.     cury++;
  271.     return(0);
  272. }
  273.  
  274. /*
  275.  * Flexible pager: feed it with a number of lines and it will decide
  276.  * whether these should be fed to the pager above, or displayed in a
  277.  * corner.
  278.  * Call:
  279.  *    cornline(0, title or 0) : initialize
  280.  *    cornline(1, text)       : add text to the chain of texts
  281.  *    cornline(2, morcs)      : output everything and cleanup
  282.  *    cornline(3, 0)          : cleanup
  283.  */
  284.  
  285. cornline(mode, text)
  286. int mode;
  287. char *text;
  288. {
  289.     static struct line {
  290.         struct line *next_line;
  291.         char *line_text;
  292.     } *texthead, *texttail;
  293.     static int maxlen;
  294.     static int linect;
  295.     register struct line *tl;
  296.  
  297.     if(mode == 0) {
  298.         texthead = 0;
  299.         maxlen = 0;
  300.         linect = 0;
  301.         if(text) {
  302.             cornline(1, text);      /* title */
  303.             cornline(1, "");        /* blank line */
  304.         }
  305.         return;
  306.     }
  307.  
  308.     if(mode == 1) {
  309.         register int len;
  310.  
  311.         if(!text) return;   /* superfluous, just to be sure */
  312.         linect++;
  313.         len = strlen(text) + 1; /* allow for an extra leading space */
  314.         if(len > maxlen)
  315.         maxlen = len;
  316.         tl = (struct line *)
  317.         alloc((unsigned)(len + sizeof(struct line) + 1));
  318.         tl->next_line = 0;
  319.         tl->line_text = (char *)(tl + 1);
  320.         tl->line_text[0] = ' ';
  321.         tl->line_text[1] = '\0';
  322.         (void) strcat(tl->line_text, text);
  323.         if(!texthead)
  324.         texthead = tl;
  325.         else
  326.         texttail->next_line = tl;
  327.         texttail = tl;
  328.         return;
  329.     }
  330.  
  331.     /* --- now we really do it --- */
  332.     if(mode == 2 && linect == 1)                        /* topline only */
  333.         pline(texthead->line_text);
  334.     else
  335.     if(mode == 2) {
  336.         register int curline, lth;
  337.  
  338.         if(flags.toplin == 1) more();       /* ab@unido */
  339.         remember_topl();
  340.  
  341.         lth = CO - maxlen - 2;           /* Use full screen width */
  342.         if (linect < LI && lth >= 10) {                  /* in a corner */
  343.         home ();
  344.         cl_end ();
  345.         flags.toplin = 0;
  346.         curline = 1;
  347.         for (tl = texthead; tl; tl = tl->next_line) {
  348. #ifdef AMIGA
  349. #undef MSDOS    /* Watch out! */
  350. #endif
  351. #ifdef MSDOS
  352.             cmov (lth, curline);
  353. #else
  354.             curs (lth, curline);
  355. #endif
  356.             if(curline > 1)
  357.             cl_end ();
  358.             xputs(tl->line_text);
  359.             curx = curx + strlen(tl->line_text);
  360.             curline++;
  361.         }
  362. #ifdef MSDOS
  363.         cmov (lth, curline);
  364. #else
  365.         curs (lth, curline);
  366. #endif
  367.         cl_end ();
  368.         cmore (text);
  369.         home ();
  370.         cl_end ();
  371.         docorner (lth, curline-1);
  372.         } else {                    /* feed to pager */
  373.         set_pager(0);
  374.         for (tl = texthead; tl; tl = tl->next_line) {
  375.             if (page_line (tl->line_text)) {
  376.             set_pager(2);
  377.             goto cleanup;
  378.             }
  379.         }
  380.         if(text) {
  381.             cgetret(text);
  382.             set_pager(2);
  383.         } else
  384.             set_pager(1);
  385.         }
  386.     }
  387.  
  388. cleanup:
  389.     while(tl = texthead) {
  390.         texthead = tl->next_line;
  391.         free((char *) tl);
  392.     }
  393. }
  394.  
  395. dohelp()
  396. {
  397.     char c;
  398.  
  399.     pline ("Long or short help? ");
  400.     while (((c = readchar ()) != 'l') && (c != 's') && !index(quitchars,c))
  401.         bell ();
  402.     if (!index(quitchars, c))
  403.         (void) page_file((c == 'l') ? HELP : SHELP, FALSE);
  404.     return(0);
  405. }
  406.  
  407. page_file(fnam, silent) /* return: 0 - cannot open fnam; 1 - otherwise */
  408. register char *fnam;
  409. boolean silent;
  410. {
  411. #ifdef DEF_PAGER            /* this implies that UNIX is defined */
  412.       {
  413.     /* use external pager; this may give security problems */
  414.  
  415.     register int fd = open(fnam, 0);
  416.  
  417.     if(fd < 0) {
  418.         if(!silent) pline("Cannot open %s.", fnam);
  419.         return(0);
  420.     }
  421.     if(child(1)){
  422.         extern char *catmore;
  423.  
  424.         /* Now that child() does a setuid(getuid()) and a chdir(),
  425.            we may not be able to open file fnam anymore, so make
  426.            it stdin. */
  427.         (void) close(0);
  428.         if(dup(fd)) {
  429.             if(!silent) printf("Cannot open %s as stdin.\n", fnam);
  430.         } else {
  431.             execl(catmore, "page", (char *) 0);
  432.             if(!silent) printf("Cannot exec %s.\n", catmore);
  433.         }
  434.         exit(1);
  435.     }
  436.     (void) close(fd);
  437.       }
  438. #else
  439.       {
  440.     FILE *f;            /* free after Robert Viduya */
  441.  
  442.     if ((f = fopen (fnam, "r")) == (FILE *) 0) {
  443.         if(!silent) {
  444.             home(); perror (fnam); flags.toplin = 1;
  445.             pline ("Cannot open %s.", fnam);
  446.         }
  447.         return(0);
  448.     }
  449.     page_more(f, 0);
  450.       }
  451. #endif /* DEF_PAGER /**/
  452.  
  453.     return(1);
  454. }
  455.  
  456. #ifdef UNIX
  457. #ifdef SHELL
  458. dosh(){
  459. register char *str;
  460.     if(child(0)) {
  461.         if(str = getenv("SHELL"))
  462.             execl(str, str, (char *) 0);
  463.         else
  464.             execl("/bin/sh", "sh", (char *) 0);
  465.         pline("sh: cannot execute.");
  466.         exit(1);
  467.     }
  468.     return(0);
  469. }
  470. #endif /* SHELL /**/
  471.  
  472. child(wt) {
  473. register int f = fork();
  474.     if(f == 0){             /* child */
  475.         settty((char *) 0);             /* also calls end_screen() */
  476.         (void) setuid(getuid());
  477.         (void) setgid(getgid());
  478. #ifdef CHDIR
  479.         (void) chdir(getenv("HOME"));
  480. #endif
  481.         return(1);
  482.     }
  483.     if(f == -1) {   /* cannot fork */
  484.         pline("Fork failed. Try again.");
  485.         return(0);
  486.     }
  487.     /* fork succeeded; wait for child to exit */
  488.     (void) signal(SIGINT,SIG_IGN);
  489.     (void) signal(SIGQUIT,SIG_IGN);
  490.     (void) wait((int *) 0);
  491.     gettty();
  492.     setftty();
  493.     (void) signal(SIGINT,done1);
  494. #ifdef WIZARD
  495.     if(wizard) (void) signal(SIGQUIT,SIG_DFL);
  496. #endif
  497.     if(wt) getret();
  498.     docrt();
  499.     return(0);
  500. }
  501. #endif /* UNIX /**/
  502.